/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package EstilosWebUI.resources;

import java.io.File;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Emmanuel
 */
public class FileExplorer implements Serializable {

    private String rutaInicial;
    private FileExplorer[] hijos;
    private String separador;
    private static FileExplorer[] CHILDREN_ABSENT = new FileExplorer[0];
    private FileExplorer[] children;

    public FileExplorer(String rutaInicial, String separador) {
        this.rutaInicial = rutaInicial;
        this.separador = separador;
    }

    public FileExplorer[] getSubCarpetas() {
        List<File> dirs = new ArrayList<File>();
        File[] tmp = new File(rutaInicial).listFiles();
        if(tmp==null){
            return new FileExplorer[0];
        }
        for (File f : tmp) {
            if (f.isDirectory()) {
                dirs.add(f);
            }
        }
        File[] carpetas = dirs.toArray(new File[0]);
        FileExplorer[] subcarpetas = new FileExplorer[carpetas.length];
        if (subcarpetas.length == 0) {
            return new FileExplorer[0];
        }
        for (int i = 0; i < carpetas.length; i++) {
            System.out.println(carpetas[i].getAbsolutePath());
            subcarpetas[i] = new FileExplorer(carpetas[i].getAbsolutePath(), this.separador);
        }
        return subcarpetas;
    }

//    public synchronized FileExplorer[] getSubCarpetas() {
//        if (children == null) {
//            File[] subcarpetas = new File(rutaInicial,this.separador).listFiles();
//            if (subcarpetas != null) {
//                children = new FileExplorer[subcarpetas.length];
//
//                for (int i = 0; i < subcarpetas.length; i++) {
//                    String nodePath = subcarpetas[i].getAbsolutePath();
//                    if (nodePath.endsWith(this.separador)) {
//                        nodePath = nodePath.substring(0, nodePath.length() - 1);
//                    }
//                    children[i] = new FileExplorer(nodePath,this.separador);
//                }
//            } else {
//                children = CHILDREN_ABSENT;
//            }
//        }
//
//        return children;
//    }
    public File[] getSubArchivos(String rutaNodoSeleccionado) {
        File[] subcarpetas = new File(rutaNodoSeleccionado).listFiles();
        return subcarpetas;
    }

    @Override
    public String toString() {
        return rutaInicial.substring(rutaInicial.lastIndexOf(this.separador) + 1);
    }
}
